package com.azusa.ktc_user.controller;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import com.azusa.ktc_user.entity.User;
import com.azusa.ktc_user.service.UserService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;

/**
 * @Description 用户 控制器层
 * @author Azusa
 * @date 2020-10-12 18:33:46
*/
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController{

    @Autowired
    private UserService userService;

    /**
    * 查询全部数据
    * @return
    */
    @RequestMapping(method= RequestMethod.GET)
    public Result findAll(){
        return new Result(StatusCode.SUCCESS,userService.findAll());
    }

    /**
    * 根据ID查询
    * @param id ID
    * @return
    */
    @RequestMapping(value="/{id}",method= RequestMethod.GET)
    public Result findById(@PathVariable String id){
        return new Result(StatusCode.SUCCESS,userService.findById(id));
    }

    /**
    * 分页+多条件查询
    * @param searchMap 查询条件封装
    * @param page 页码
    * @param size 页大小
    * @return 分页结果
    */
    @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
        public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
        Page<User> pageList = userService.findSearch(searchMap, page, size);
        return  new Result(StatusCode.SUCCESS, new PageResult<User>(pageList.getTotalElements(), pageList.getContent()) );
    }

    /**
    * 根据条件查询
    * @param searchMap
    * @return
    */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){
        return new Result(StatusCode.SUCCESS, userService.findSearch(searchMap));
    }

    /**
    * 增加
    * @param user
    */
    @RequestMapping(method=RequestMethod.POST)
    public Result add(@RequestBody  User  user  ){
        userService.add( user);
        return new Result(StatusCode.SUCCESS);
    }

    /**
    * 修改
    * @param user
    */
    @RequestMapping(value="/{id}",method= RequestMethod.PUT)
    public Result update(@RequestBody  User  user, @PathVariable String id ){
        user.setId(id);
        userService.update( user);
        return new Result(StatusCode.SUCCESS);
    }

    /**
    * 删除
    * @param id
    */
    @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
    public Result delete(@PathVariable String id ){
        userService.deleteById(id);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 发送验证码到用户手机上
     * @param mobile
     * @return
     */
    @GetMapping("sendsms/{mobile}")
    public Result sendSms(@PathVariable("mobile") String mobile){
        userService.sendSms(mobile);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 注册用户
     * @param code
     * @param user
     * @return
     */
    @PostMapping("register/{code}")
    public Result register(@PathVariable("code") String code,@RequestBody User user){
        return userService.register(code,user);
    }

    /**
     * 登录用户
     * @param user
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody User user){
        return userService.login(user);
    }

    /**
     * 修改粉丝数
     * @param userId
     * @param count
     * @return
     */
    @PutMapping("/updateFansCount/{userId}/{count}")
    public Result updateFansCount(@PathVariable("userId") String userId,@PathVariable("count") Integer count){
        userService.updateFons(userId, count);
        return new Result(StatusCode.SUCCESS,"修改成功");
    }

    /**
     * 修改关注数
     * @param userId
     * @param count
     * @return
     */
    @PutMapping("/updateFollowCount/{userId}/{count}")
    public Result updateFollowCount(@PathVariable("userId") String userId,@PathVariable("count") Integer count) {
        userService.updateFollowCount(userId, count);
        return new Result(StatusCode.SUCCESS,"修改成功");
    }

}
